<template>
  <view 
    class="float-button" 
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
    :style="{ left: position.left + 'px', top: position.top + 'px' }"
    @click="handleClick"
  >
    <text>更多</text>
  </view>
</template>

<script>
export default {
  data() {
    return {
      position: {
        left: 0,
        top: 0
      },
      startX: 0,
      startY: 0,
      screenWidth: 0,
      screenHeight: 0
    }
  },
  mounted() {
    this.initPosition()
    this.getScreenSize()
  },
  methods: {
    initPosition() {
      // 初始位置在右下角
      const { windowWidth, windowHeight } = uni.getSystemInfoSync()
      this.position = {
        left: windowWidth - 60,
        top: windowHeight - 200
      }
    },
    getScreenSize() {
      const { windowWidth, windowHeight } = uni.getSystemInfoSync()
      this.screenWidth = windowWidth
      this.screenHeight = windowHeight
    },
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX - this.position.left
      this.startY = e.touches[0].clientY - this.position.top
    },
    handleTouchMove(e) {
      // 阻止事件冒泡，避免拖动时触发页面滚动
      e.stopPropagation()
      
      const clientX = e.touches[0].clientX - this.startX
      const clientY = e.touches[0].clientY - this.startY
      
      // 限制拖动范围
      this.position.left = Math.max(0, Math.min(clientX, this.screenWidth - 120))
      this.position.top = Math.max(0, Math.min(clientY, this.screenHeight - 120))
    },
    handleTouchEnd() {
      // 吸边效果
      const middleX = this.screenWidth / 2
      if (this.position.left + 60 < middleX) {
        this.position.left = 0 // 吸附到左边
      } else {
        this.position.left = this.screenWidth - 60 // 吸附到右边
      }
    },
    handleClick() {
      uni.switchTab({
        url: '/pages/record/index'
      })
    }
  }
}
</script>

<style scoped>
.float-button {
  position: fixed;
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  background: linear-gradient(135deg, v-bind('primaryColor') 0%, v-bind('secondaryColor') 100%);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.2);
  z-index: 999;
  touch-action: none;
  font-weight: bold;
  font-size: 28rpx;
  transition: all 0.3s ease;
}

.float-button:active {
  transform: scale(0.95);
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.15);
}
</style>